package test;
import common.Utils;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-09-05
 * Time:19:47
 */
public class GameRoomPage extends Utils {
    public static String url = "http://81.70.248.108:58081/game_room.html";

    public GameRoomPage(boolean isGet) {

        super(isGet ? url: null);
    }
    /**
     * 检查游戏房间界面元素是否存在
     */
    public void checkGameRoom(){
        //检查棋盘
        webDriver.findElement(By.cssSelector("#chess"));
        //检查提示落子信息
        webDriver.findElement(By.cssSelector("#screen"));
        String message = webDriver.findElement(By.cssSelector("#screen")).getText();
        System.out.println(message);
        System.out.println("游戏加载完成");
    }

    public void game() throws IOException, InterruptedException {
        //等待人工下棋
        Scanner scanner = new Scanner(System.in);
        System.out.println("开始人工下棋");
        System.out.println("提示：现在正在人工落子，游戏结束请输入over ,结束这局对弈");
        while (true){
            String input = scanner.nextLine().trim();
            if ("over".equals(input)){
                System.out.println("人工下棋结束，开始自动化");
                break;
            }else{
                System.out.println("输入无效");
            }
        }
        scanner.close();

        //未出现回到大厅按钮游戏未结束
        WebDriverWait wait1 = new WebDriverWait(webDriver,Duration.ofSeconds(3));
        try{
            //游戏结束等待返回大厅按钮的出现
            WebElement returnBtn = wait1.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#return-btn")));
            returnBtn.click();

            //等待页面跳转完成
            WebDriverWait titleWait = new WebDriverWait(webDriver,Duration.ofSeconds(3));
            titleWait.until(ExpectedConditions.titleIs("游戏大厅"));

            //验证是否返回游戏大厅
            String title = webDriver.getTitle();
            assert title.equals("游戏大厅"):"返回游戏大厅失败";
        }catch (TimeoutException e){
            System.out.println("等待游戏大厅按钮超时");
//            System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
//            ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        }
        catch (UnhandledAlertException e){
            System.out.println("连接游戏失败");
            System.out.println(Thread.currentThread().getStackTrace()[2].getMethodName());
            try{
                Alert alert = webDriver.switchTo().alert();
                String alertMessage = alert.getText();
                System.out.println(alertMessage);
                Thread.sleep(2000);
                alert.dismiss();
                System.out.println("已关闭弹窗");
            }catch (NoAlertPresentException alertEx){
                System.out.println("弹窗已消失");
            }

            WebDriverWait gameHallWait = new WebDriverWait(webDriver,Duration.ofSeconds(3));
            gameHallWait.until(ExpectedConditions.titleIs("游戏大厅"));
            System.out.println("已返回游戏大厅，请重新开始游戏");
        }

    }

    public void unLogin(){
        WebDriverWait alertWait = new WebDriverWait(webDriver,Duration.ofSeconds(3));
        alertWait.until(ExpectedConditions.alertIsPresent());
        //切换到弹窗
        Alert alert = webDriver.switchTo().alert();
        alert.dismiss();
    }
}
